<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleClose">
    <div class="modal-content">
      <div class="modal-header">
        <h3>页面属性</h3>
        <button class="close-btn" @click="handleClose">
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
            <path d="M15.8333 4.16667L4.16666 15.8333M4.16666 4.16667L15.8333 15.8333" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
      </div>
      
      <div class="modal-body">
        <!-- 基本设置 -->
        <div class="setting-section">
          <h4>基本设置</h4>
          
          <div class="form-group">
            <label for="page-name">页面名称</label>
            <input
              id="page-name"
              type="text"
              v-model="pageData.name"
              class="form-input"
              placeholder="请输入页面名称"
              disabled
            />
            <div class="form-note">页面名称不可修改，请使用重命名功能</div>
          </div>
          
          <div class="form-group">
            <label for="page-title">页面标题</label>
            <input
              id="page-title"
              type="text"
              v-model="pageData.title"
              class="form-input"
              placeholder="请输入页面标题"
            />
            <div class="form-note">显示在浏览器标签和搜索结果中的标题</div>
          </div>
          
          <div class="form-group">
            <label for="page-route">页面路由</label>
            <input
              id="page-route"
              type="text"
              v-model="pageData.route"
              class="form-input"
              placeholder="请输入页面路由，以/开头"
            />
            <div class="form-note">访问页面的URL路径，必须以斜杠(/)开头</div>
          </div>
        </div>
        
        <!-- SEO设置 -->
        <div class="setting-section">
          <h4>SEO设置</h4>
          
          <div class="form-group">
            <label for="seo-title">SEO标题</label>
            <input
              id="seo-title"
              type="text"
              v-model="pageData.seo.title"
              class="form-input"
              placeholder="请输入SEO标题"
            />
            <div class="form-note">在搜索结果中显示的标题，建议不超过60个字符</div>
          </div>
          
          <div class="form-group">
            <label for="seo-description">SEO描述</label>
            <textarea
              id="seo-description"
              v-model="pageData.seo.description"
              class="form-textarea"
              placeholder="请输入SEO描述"
              rows="3"
            ></textarea>
            <div class="form-note">在搜索结果中显示的描述，建议不超过160个字符</div>
          </div>
          
          <div class="form-group">
            <label for="seo-keywords">SEO关键词</label>
            <input
              id="seo-keywords"
              type="text"
              :value="keywordsText"
              @input="handleKeywordsInput"
              class="form-input"
              placeholder="请输入关键词，用逗号分隔"
            />
            <div class="form-note">页面的核心关键词，用于搜索引擎识别页面内容</div>
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn-cancel" @click="handleClose">取消</button>
        <button class="btn-save" @click="handleSave">保存</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {  defineEmits, computed, ref, watch } from 'vue';

// Props定义
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  page: {
    type: Object,
    default: () => ({
      id: '',
      name: '',
      title: '',
      route: '',
      seo: {
        title: '',
        description: '',
        keywords: []
      }
    })
  }
});

// Emits定义
const emit = defineEmits(['close', 'save']);

// 内部状态，使用深拷贝避免直接修改props
const pageData = ref({
  id: '',
  name: '',
  title: '',
  route: '',
  seo: {
    title: '',
    description: '',
    keywords: []
  }
});

// 监听props变化，更新内部状态
watch(
  () => props.page,
  (newPage) => {
    if (newPage) {
      pageData.value = {
        ...newPage,
        seo: { ...newPage.seo || { title: '', description: '', keywords: [] } }
      };
    }
  },
  { immediate: true, deep: true }
);

// 将关键词数组转换为逗号分隔的字符串
const keywordsText = computed(() => {
  return pageData.value.seo.keywords.join(', ');
});

// 处理关键词输入
const handleKeywordsInput = (event) => {
  const value = event.target.value;
  // 以逗号分隔关键词，去除空白，并过滤空字符串
  pageData.value.seo.keywords = value.split(',').map(keyword => keyword.trim()).filter(keyword => keyword);
};

// 关闭模态框
const handleClose = () => {
  emit('close');
};

// 保存页面属性
const handleSave = () => {
  // 验证路由格式
  if (pageData.value.route && !pageData.value.route.startsWith('/')) {
    alert('路由必须以斜杠(/)开头');
    return;
  }
  
  // 验证SEO标题长度
  if (pageData.value.seo.title && pageData.value.seo.title.length > 60) {
    if (!confirm('SEO标题长度超过60个字符，可能会影响搜索引擎显示效果，确定继续吗？')) {
      return;
    }
  }
  
  // 验证SEO描述长度
  if (pageData.value.seo.description && pageData.value.seo.description.length > 160) {
    if (!confirm('SEO描述长度超过160个字符，可能会影响搜索引擎显示效果，确定继续吗？')) {
      return;
    }
  }
  
  emit('save', pageData.value);
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.modal-content {
  background-color: white;
  border-radius: 8px;
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  animation: slideUp 0.3s ease;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e2e8f0;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #334155;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #64748b;
  padding: 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background-color: #f1f5f9;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.setting-section {
  margin-bottom: 30px;
}

.setting-section:last-child {
  margin-bottom: 0;
}

.setting-section h4 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #334155;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 500;
  color: #334155;
}

.form-input,
.form-textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.form-input:focus,
.form-textarea:focus {
  outline: none;
  border-color: #165dff;
  box-shadow: 0 0 0 3px rgba(22, 93, 255, 0.1);
}

.form-input:disabled {
  background-color: #f3f4f6;
  cursor: not-allowed;
}

.form-textarea {
  resize: vertical;
  min-height: 80px;
}

.form-note {
  margin-top: 4px;
  font-size: 12px;
  color: #64748b;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
  background-color: #f8fafc;
}

.btn-cancel,
.btn-save {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-cancel {
  background-color: #f3f4f6;
  color: #334155;
}

.btn-cancel:hover {
  background-color: #e5e7eb;
}

.btn-save {
  background-color: #165dff;
  color: white;
}

.btn-save:hover {
  background-color: #0d47a1;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>